﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace DAL
{
    public class GetDataByVW_NameForWebServiceDAL :DB
    {
        public GetDataByVW_NameForWebServiceDAL(LoginInfo info)
        {
            base.loginInfo = info;
        }
        

        /// <summary>
        /// 根据传入的视图名称返回结果集，当前方法为测试方法，需改进
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="vwName">使他们</param>
        /// <returns>结果集</returns>
        public DataTable GetData(string userName, string schemaName, string vwName)
        {
            HIDB.dbo conn = base.conn();
            string vw_Name;
            //vw_Name = conn.GetScalar("select getdate()");
            try
            {
                vw_Name = conn.GetScalar("select a.name from sys.objects a inner join Working_db.SysView_Config b on SCHEMA_NAME(a.schema_id)=b.ViewSchema and a.name=b.ViewName inner join working_db.ETL_Push_ViewGroup c on v.id=c.viewid where a.name = @name and SCHEMA_NAME(a.schema_id)=@schema and c.IsWebServicePush=1", new SqlParameter[] { new SqlParameter("@name", vwName), new SqlParameter("@schema", schemaName) });
            }
            catch (Exception)
            {
                return null;
            }
            DataTable dt = conn.GetDataTable("select * from [" + schemaName + "].[" + vw_Name + "]");
            dt.TableName = vwName;
            return dt;
        }

        /// <summary>
        /// 根据传入的视图名称返回结果集，当前方法为测试方法，需改进
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="vwName">使他们</param>
        /// <returns>结果集</returns>
        public int GetViewPushType(string userName, string schemaName, string vwName)
        {
            HIDB.dbo conn = base.conn();
            string vw_Name;
            //vw_Name = conn.GetScalar("select getdate()");
            try
            {
                vw_Name = conn.GetScalar("select a.name from sys.objects a inner join Working_db.SysView_Config b on SCHEMA_NAME(a.schema_id)=b.ViewSchema and a.name=b.ViewName inner join working_db.ETL_Push_ViewGroup c on v.id=c.viewid where a.name = @name and SCHEMA_NAME(a.schema_id)=@schema and c.IsWebServicePush=1", new SqlParameter[] { new SqlParameter("@name", vwName), new SqlParameter("@schema", schemaName) });
            }
            catch (Exception)
            {
                return 0;
            }

            if (vw_Name.Length == 0)
            {
                return 0;
            }
            return 1;
        }

        /// <summary>
        /// 获取对接系统的线程数
        /// </summary>
        /// <param name="userName">对接系统CODE</param>
        /// <returns>返回threads</returns>
        public int GetThreads(string userName)
        {
            HIDB.dbo conn = base.conn();
            int threads;
            try
            {
                int.TryParse(conn.GetScalar("select a.Thread from Working_db.ETL_TargetSystem  a where a.TargetSysCode = @code", new SqlParameter[] {
                new SqlParameter("@code",userName)
            }), out threads);
            }
            catch (Exception)
            {
                return -1;
            }

            return threads;
        }

    }
}
